#include <stdio.h>
#include <sstream>
#include <iostream>

#include "graph.h"
#include "algorithm.h"
#include "deploy.h"

using namespace std;

//你要完成的功能总入口
void deploy_server(char * topo[MAX_EDGE_NUM], int line_num,char * filename)
{
	istringstream is;

	int i, j;
	int node_cnt = 0;
	int link_cnt = 0;
	int cnode_cnt = 0;
	int server_cost = 0;

	is.str(topo[0]);
	is >> node_cnt >> link_cnt >> cnode_cnt;

	cout << "Node Count = " << node_cnt << "||Link Count = " << link_cnt << "||Consumer Count = " << cnode_cnt << endl;

	is.str(topo[2]);
	is >> server_cost;

	cout << "server_cost = " << server_cost << endl;

	Graph graph(node_cnt, link_cnt, cnode_cnt);

	// 初始化节点信息
	for (i = 0; i < node_cnt; i++) {
		Node n(i);
		graph.addNode(n);
	}

	int src = 0;
	int dst = 0;
	int bw = 0;
	int price = 0;
	for (i = 4; i < 4 + link_cnt; i++) {
		is.str(topo[i]);
		is >> src >> dst >> bw >> price;
		Link l(i - 4, src, dst, bw, bw, price);
		graph.addLink(l);
	}

	int nid = 0;
	int cid = 0;
	int consume = 0;
	for (i = 5 + link_cnt; i < 5 + link_cnt + cnode_cnt; i++) {
		is.str(topo[i]);
		is >> cid >> nid >> consume;
		graph.getNodeById(nid).setConsume(cid, consume);
	}

	//graph.printGraph();

	vector<vector<int> > pathset;
	Algorithm::run(graph, server_cost, pathset);

	int path_cnt = pathset.size();

	ostringstream os;
	os << path_cnt << "\n\n";

	for (i = 0; i < path_cnt; i++) {
		for (j = 0; j < pathset[i].size(); j++) {
			os << pathset[i][j] << " ";
		}

		if (i < path_cnt - 1)
			os << "\n";
	}

	write_result(os.str().c_str(), filename);

	// 直接调用输出文件的方法输出到指定文件中(ps请注意格式的正确性，如果有解，第一行只有一个数据；第二行为空；第三行开始才是具体的数据，数据之间用一个空格分隔开)
	//write_result(topo_file, filename);
}
